﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Specialized;

namespace AllNations.Controls
{
    public partial class StaticMainImage : MakeItWeb.Framework.MakeItWebUserControl
    {
        public string MainImageUrl { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (string.IsNullOrEmpty(MainImageUrl))
                MainImageUrl = GetStaticMainImage();

            imgStaticMainImage.ImageUrl = MainImageUrl;
        }

        private string GetStaticMainImage()
        {
            var images = ConfigurationManager.GetSection("StaticMainImages") as NameValueCollection;
            var idx = 0;
            if (Session["StaticImageIndex"] == null)
            {
                var r = new Random();
                idx = r.Next(0, images.Count);
                Session["StaticImageIndex"] = idx;
            }
            else
                idx = (int)Session["StaticImageIndex"];

            return images[idx];
        }
    }
}